package com.yu.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yu.crm.domain.pojo.CrmEarlyWarningDiscern;
import com.yu.crm.domain.pojo.CrmEarlyWarningDiscernArea;
import com.yu.crm.mapper.CrmEarlyWarningDiscernMapper;
import com.yu.crm.service.ICrmEarlyWarningDiscernService;
import com.yxzz.common.config.UserCacheManager;
import com.yxzz.common.helper.DataHelpers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author pan
 * @since 2020-07-01
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class CrmEarlyWarningDiscernService extends ServiceImpl<CrmEarlyWarningDiscernMapper, CrmEarlyWarningDiscern> implements ICrmEarlyWarningDiscernService {
    @Autowired
    UserCacheManager userCacheManager;
    @Autowired
    CrmEarlyWarningDiscernAreaService discernAreaService;
    @Override
    public void addOrUpdate(CrmEarlyWarningDiscern earlyWarningDiscern) {
        if(DataHelpers.isNotEmpty(earlyWarningDiscern.getId())){
            earlyWarningDiscern.setGmtModified(LocalDateTime.now());
            this.updateById(earlyWarningDiscern);
        }else{
            earlyWarningDiscern.setCreateUserId(userCacheManager.getCurrentUser().getUserId());
            earlyWarningDiscern.setGmtCreate(LocalDateTime.now());
            earlyWarningDiscern.setGmtModified(LocalDateTime.now());
            this.save(earlyWarningDiscern);
        }
    }

    @Override
    public List<CrmEarlyWarningDiscern> listInfo() {
        String userId = userCacheManager.getCurrentUser().getUserId();
        QueryWrapper<CrmEarlyWarningDiscern> wrapper = new QueryWrapper<>();
        wrapper.eq("create_user_id",userId);
        return this.list(wrapper);
    }

    @Override
    public void removeDiscern(Long id) {
        this.removeById(id);
        QueryWrapper<CrmEarlyWarningDiscernArea> wrapper = new QueryWrapper<>();
        wrapper.eq("early_warning_discern_id",id);
        discernAreaService.remove(wrapper);
    }
}
